set more off, permanently

* --- Load banks data and inititialize variables --- *

clear all
insheet using "dsbanksfull.csv"

rename filingdate sfilingdate

gen obsdate = date(sobsdate, "MD20Y")
gen obsquarter = qofd(obsdate)
gen filingdate = date(sfilingdate, "MD20Y")
gen treated = filingdate < obsdate
gen ttreatment = obsdate - filingdate if treated==1

rename eligible eligible_sh
gen eligible = obsdate > date("4/5/2012", "MDY") & netsh < 1200
gen complier = eligible==treated
gen teligible = eligible*ttreatment

*rename netsh old_netsh
*gen netsh = old_netsh - 1200
gen netsh2 = netsh*netsh
gen netsh3 = netsh2*netsh
gen netsh4 = netsh3*netsh
gen t_netsh = netsh*treated
gen t_netsh2 = netsh2*treated
gen t_netsh3 = netsh3*treated
gen t_netsh4 = netsh4*treated
gen e_netsh = netsh*eligible
gen e_netsh2 = netsh2*eligible
gen e_netsh3 = netsh3*eligible
gen e_netsh4 = netsh4*eligible

gen absnetsh = abs(netsh)
egen maxabsnetsh = max(absnetsh)
gen weightnetsh = maxabsnetsh - absnetsh

rename uniqueid suniqueid
encode suniqueid, generate(uniqueid)
tsset uniqueid obsquarter, quarterly 

* --- Label variables --- *

label variable bhcid "BHC ID"
label variable bankcertnum "Bank Certificate Num."
label variable company "Company"
label variable dereg "Deregistration (1/0)"
label variable eligible "Eligible for Deregistration (1/0)"
label variable netsh "Shareholders of Record"
label variable filingsh "Shareholders of Record at Deregistration"
label variable totalpretaxinc "Total Pretax Income"
label variable totalpretaxexp "Total Pretax Expenses"
label variable ubpre013 "Net Income"
label variable totalniexp "Total Noninterest Expenses"
label variable legalacctexp "Legal & Acct/Audit Expenses"
label variable ubpre088 "Efficiency Ratio"
label variable ubpre090 "Assets per Employee"
label variable ubprj245 "Capital Ratio"
label variable obsquarter "Observation Quarter"

* --- Summary statistics --- *

sutex bhcid bankcertnum dereg eligible netsh filingsh totalpretaxinc totalpretaxexp ubpre013 totalniexp legalacctexp ubpre088 ubpre090 ubprj245, label digits(2) minmax 

* --- McCrary density test --- *

* Graph at 900 *

local bw 520.454186321052
DCdensity netsh if (obsquarter==q(2012q4) & netsh > 1200-`bw' & netsh < 1200+`bw'), breakpoint(1200) h( 520.454186321052) generate(Xj Yj r0 fhat se_fhat)
drop Xj Yj r0 fhat se_fhat

* Use McCrary bandwidth selection *

DCdensity netsh if (obsquarter==q(2012q4) & netsh > 1200-900 & netsh < 1200+900), breakpoint(1200) generate(Xj Yj r0 fhat se_fhat) nograph
drop Xj Yj r0 fhat se_fhat
scalar t = r(theta) / r(se)
scalar list t
return list

* Loop through tables to show robustness to choice of bandwidth *

noi foreach bw in 100 200 300 400 500 600 700 800 900 {

DCdensity netsh if (obsquarter==q(2012q4) & netsh > 1200-`bw' & netsh < 1200+`bw'), breakpoint(1200) h(`bw') generate(Xj Yj r0 fhat se_fhat) nograph
drop Xj Yj r0 fhat se_fhat
scalar t = r(theta) / r(se)
scalar list t

}

* T-Tests for # of shareholders *

ttest netsh==sh2011 if obsquarter==q(2012q4) & dereg==1
ttest netsh==sh2010 if obsquarter==q(2012q4) & dereg==1

* --- RD estimations on level outcomes --- *

noi foreach bw in 100 200 300 400 500 600 700 800 900 {

estimates clear
graph drop _all
set graphics off

noi foreach outvar in totalpretaxinc totalpretaxexp ubpre013 totalniexp legalacctexp ubpre088 ubpre090 ubprj245 {

ivreg2 `outvar' netsh* (treated=eligible) if (netsh > 1200-`bw' & netsh < 1200+`bw'), cluster(uniqueid obsquarter)
estimates store m`outvar'

if (`bw'==300 | `bw' == 600 | `bw' == 900) {

predict xb, xb
predict stdp, stdp
gen xb_ci1 = xb - invttail(_N-1, .025)*stdp
gen xb_ci2 = xb + invttail(_N-1, .025)*stdp

line xb netsh if (netsh > (1200-`bw') & netsh < (1200+`bw')  & treated==1 & complier==1), sort lcolor(red) || /// 
line xb netsh if (netsh > (1200-`bw') & netsh < (1200+`bw')  & treated==0 & complier==1), sort lcolor(blue) || ///
line xb_ci1 netsh if (netsh > (1200-`bw') & netsh < (1200+`bw') & treated==1 & complier==1), sort lpattern(dash) lcolor(red) lwidth(thin) || /// 
line xb_ci1 netsh if (netsh > (1200-`bw') & netsh < (1200+`bw') & treated==0 & complier==1), sort lpattern(dash) lcolor(blue) lwidth(thin) || /// 
line xb_ci2 netsh if (netsh > (1200-`bw') & netsh < (1200+`bw') & treated==1 & complier==1), sort lpattern(dash) lcolor(red) lwidth(thin) || /// 
line xb_ci2 netsh if (netsh > (1200-`bw') & netsh < (1200+`bw') & treated==0 & complier==1), sort lpattern(dash) lcolor(blue) lwidth(thin) ///
xline(1200, lpattern(dash) lcolor(gray) lwidth(thin)) ///
legend(label(1 "Treatment") label(2 "Control") label(3 "95% CI%") label(4 "95% CI%") ///
label(5 "95% CI%") label(6 "95% CI%")) name(g_`outvar') title(`: variable label `outvar'')  scheme(s1color)

drop xb stdp xb_ci1 xb_ci2

}

}

set graphics on

if (`bw'==300 | `bw' == 600 | `bw' == 900) {
grc1leg g_totalpretaxinc g_totalpretaxexp g_ubpre013 g_totalniexp, legendfrom(g_totalpretaxinc) saving(graph`bw'1, replace) scheme(s1color)
graph export graph`bw'1.eps, replace
grc1leg g_legalacctexp g_ubpre088 g_ubpre090 g_ubprj245, legendfrom(g_legalacctexp) saving(graph`bw'2, replace) scheme(s1color)
graph export graph`bw'2.eps, replace

}

estout m* using `"estimation-banks.txt"', cells(b(fmt(a2) star) se(fmt(2) par)) ///
starlevels(`"\sym{*}"' 0.10 `"\sym{**}"' 0.05 `"\sym{***}"' 0.01, label(" \(p<@\)")) ///
varwidth(10) modelwidth(9) delimiter(&) end(\\) varlabels(treated `bw'  , end("" [1em]) nolast) ///
mlabels(none) collabels(none) append notype level(95) style(esttab) keep(treated)

}

* --- Test for joint significance of running variable polynomial --- *

noi foreach bw in 300 400 500 600 700 800 900 {

estimates clear
graph drop _all
set graphics off

noi foreach outvar in totalpretaxinc totalpretaxexp totalniexp ubpre013 legalacctexp otherexp ubpre088 ubpre090 ubprj245 {

ivreg2 `outvar' netsh* (treated=eligible) if (netsh > 1200-`bw' & netsh < 1200+`bw'), cluster(uniqueid obsquarter)
*test netsh netsh2 netsh3 netsh4

}

}

* --- Time series graphs --- *

global bwt 600

estimates clear
graph drop _all
set graphics off

noi foreach outvar in totalpretaxinc totalpretaxexp ubpre013 totalniexp legalacctexp ubpre088 ubpre090 ubprj245 {

ivreg2 `outvar' netsh* (treated=eligible) if (netsh > (1200-$bwt) & netsh < (1200+$bwt)), cluster(uniqueid)

predict xb, xb
predict stdp, stdp
gen xb_ci1 = xb - invttail(_N-1, .025)*stdp
gen xb_ci2 = xb + invttail(_N-1, .025)*stdp

sort obsquarter treated
by obsquarter treated: egen xb_meanq = median(xb)
by obsquarter treated: egen xb_ci1_meanq = median(xb_ci1)
by obsquarter treated: egen xb_ci2_meanq = median(xb_ci2)

tsline xb_meanq if (netsh > (1200-$bwt) & netsh < (1200+$bwt) & treated==1 & complier==1 & obsquarter > q(2010q1)), sort lcolor(red) tline(2012q2, lcolor(gray) lwidth(thin)) || /// 
tsline xb_meanq if (netsh > (1200-$bwt) & netsh < (1200+$bwt) & treated==0 & complier==1 & obsquarter > q(2010q1)), sort lcolor(blue) || ///
tsline xb_ci1_meanq if (netsh > (1200-$bwt) & netsh < (1200+$bwt) & treated==1 & complier==1 & obsquarter > q(2010q1)), sort lpattern(dash) lcolor(red) lwidth(thin) || /// 
tsline xb_ci1_meanq if (netsh > (1200-$bwt) & netsh < (1200+$bwt) & treated==0 & complier==1 & obsquarter > q(2010q1)), sort lpattern(dash) lcolor(blue) lwidth(thin) || /// 
tsline xb_ci2_meanq if (netsh > (1200-$bwt) & netsh < (1200+$bwt) & treated==1 & complier==1 & obsquarter > q(2010q1)), sort lpattern(dash) lcolor(red) lwidth(thin) || /// 
tsline xb_ci2_meanq if (netsh > (1200-$bwt) & netsh < (1200+$bwt) & treated==0 & complier==1 & obsquarter > q(2010q1)), sort lpattern(dash) lcolor(blue) lwidth(thin) ///
legend(label(1 "Treatment") label(2 "Control") label(3 "95% CI%") label(4 "95% CI%") ///
label(5 "95% CI%") label(6 "95% CI%"))  name(gt_`outvar') title(`: variable label `outvar'')  scheme(s1color)

drop xb stdp xb_ci1 xb_ci2 xb_meanq xb_ci1_meanq xb_ci2_meanq

}

set graphics on

grc1leg gt_totalpretaxinc gt_totalpretaxexp gt_ubpre013 gt_totalniexp, legendfrom(gt_totalpretaxinc) saving(graphtime1, replace) scheme(s1color)
graph export graphtime1.eps, replace
grc1leg gt_legalacctexp gt_ubpre088 gt_ubpre090 gt_ubprj245, legendfrom(gt_legalacctexp) saving(graphtime2, replace) scheme(s1color)
graph export graphtime2.eps, replace

* Pretreatment Covariates *

noi foreach bw in 100 200 300 400 500 600 700 800 900 {

estimates clear
graph drop _all
set graphics off

noi foreach outvar in totalpretaxinc totalpretaxexp ubpre013 totalniexp legalacctexp ubpre088 ubpre090 ubprj245 {

ivreg2 `outvar' eligible_sh netsh* if (obsdate < date("4/1/2012", "MDY") & netsh > 1200-`bw' & netsh < 1200+`bw'), cluster(uniqueid obsquarter)
estimates store m`outvar'

if (`bw'==300 | `bw' == 600 | `bw' == 900) {

predict xb, xb
predict stdp, stdp
gen xb_ci1 = xb - invttail(_N-1, .025)*stdp
gen xb_ci2 = xb + invttail(_N-1, .025)*stdp

line xb netsh if (netsh > (1200-`bw') & netsh < (1200+`bw') & eligible_sh==1), sort lcolor(red) || /// 
line xb netsh if (netsh > (1200-`bw') & netsh < (1200+`bw') & eligible_sh==0), sort lcolor(blue) || ///
line xb_ci1 netsh if (netsh > (1200-`bw') & netsh < (1200+`bw') & eligible_sh==1), sort lpattern(dash) lcolor(red) lwidth(thin) || /// 
line xb_ci1 netsh if (netsh > (1200-`bw') & netsh < (1200+`bw') & eligible_sh==0), sort lpattern(dash) lcolor(blue) lwidth(thin) || /// 
line xb_ci2 netsh if (netsh > (1200-`bw') & netsh < (1200+`bw') & eligible_sh==1), sort lpattern(dash) lcolor(red) lwidth(thin) || /// 
line xb_ci2 netsh if (netsh > (1200-`bw') & netsh < (1200+`bw') & eligible_sh==0), sort lpattern(dash) lcolor(blue) lwidth(thin) ///
xline(1200, lpattern(dash) lcolor(gray) lwidth(thin)) ///
legend(label(1 "Eligible") label(2 "Non-Eligible") label(3 "95% CI%") label(4 "95% CI%") ///
label(5 "95% CI%") label(6 "95% CI%")) name(g_`outvar') title(`: variable label `outvar'')  scheme(s1color)

drop xb stdp xb_ci1 xb_ci2

}

}

set graphics on

if (`bw'==300 | `bw' == 600 | `bw' == 900) {
grc1leg g_totalpretaxinc g_totalpretaxexp g_ubpre013 g_totalniexp, legendfrom(g_totalpretaxinc) saving(graphpt`bw'1, replace) scheme(s1color)
graph export graphpt`bw'1.eps, replace
grc1leg g_legalacctexp g_ubpre088 g_ubpre090 g_ubprj245, legendfrom(g_legalacctexp) saving(graphpt`bw'2, replace) scheme(s1color)
graph export graphpt`bw'2.eps, replace

}

estout m* using `"estimation-pretreat.txt"', cells(b(fmt(a2) star) se(fmt(2) par)) ///
starlevels(`"\sym{*}"' 0.10 `"\sym{**}"' 0.05 `"\sym{***}"' 0.01, label(" \(p<@\)")) ///
varwidth(10) modelwidth(9) delimiter(&) end(\\) varlabels(eligible_sh `bw'  , end("" [1em]) nolast) ///
mlabels(none) collabels(none) append notype level(95) style(esttab) keep(eligible_sh)

}

* Generating estimation table skeleton *
esttab m* using "estimation-banks-header.txt", keep(treated) coef(treated `bw') title("Effect of JOBS Act Deregistration on Unlisted Banks and BHCs") stats(N) se(2) b(2) star(* 0.10 ** 0.05 *** 0.01) compress tex nomtitles replace /// 
addnotes("(1) Total Pretax Income as % of Average Assets" ///
"(2) Total Pretax Expenses as % of Average Assets" ///
"(3) Net Income as % of Average Assets" ///
"(4) Other Noninterest Expenses as % of Average Assets" ///
"(5) Legal & Acct/Audit Expenses as % of Average Assets" ///
"(6) Efficiency Ratio" ///
"(7) Assets per Employee" ///
"(8) Capital Ratio")


